﻿using System;
using System.Xml;
using Cyberwolf.Modules.PWCAPAdmin.UserControls;
using Cyberwolf.Modules.PWCAPAdmin.Components;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Common;

namespace Cyberwolf.Modules.PWCAPAdmin.Components
{
    public abstract class PWCAPAdminBaseClass : PortalModuleBase
    {
        #region Public Properties
        public string NoticeFlash
        {
            get
            {
                if (Session["PageNoticeFlash"] != null)
                {
                    return Session["PageNoticeFlash"].ToString();
                }
                return null;
            }
            set { Session["PageNoticeFlash"] = value; }
        }

        public string ErrorFlash
        {
            get
            {
                if (Session["PageErrorFlash"] != null)
                {
                    return Session["PageErrorFlash"].ToString();
                }
                return null;
            }
            set { Session["PageErrorFlash"] = value; }
        }

        public abstract MainMenu Menu { get; }

        public PWApplicationService AppService { get; private set; }
        #endregion

        #region Override Methods
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            if (!Utilities.SettingsOK(Settings)) return;

            if ("LoginForm".Equals(this.ModuleConfiguration.ModuleControl.ControlKey))
            {
            }
            else
            {
                this.AppService = new PWApplicationService((string)Settings["SERVICE_URL"]);
                if (PWSecurityToken.Current == null || !this.AppService.KeepAliveSession())
                {
                    Response.Redirect(Globals.NavigateURL(this.TabId, "LoginForm", "mid=" + ModuleId, GetActiveContainerAndSkin()+ "&ReturnUrl=" + System.Web.HttpUtility.UrlEncode(this.Request.RawUrl)));
                    Response.End();
                }

                Menu.Container = this;
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            DotNetNuke.Framework.jQuery.RegisterJQuery(Page);
        }
        #endregion

        public string GetActiveContainerAndSkin()
        {
            string containerSrc = Request["containersrc"] ?? "[G]/Containers/_default/No Container";
            if (!string.IsNullOrEmpty(containerSrc)) containerSrc = "containersrc=" + Globals.QueryStringEncode(containerSrc.Replace(".ascx", "").Replace("\\", "/"));
            string skinSrc = Request["skinSrc"] ?? this.PortalSettings.ActiveTab.SkinSrc;
            if (!string.IsNullOrEmpty(skinSrc)) skinSrc = "skinsrc=" + Globals.QueryStringEncode(skinSrc.Replace(".ascx", "").Replace("\\", "/"));

            return skinSrc + "&" + containerSrc;
        }

        protected void lnkLogout_Click(object sender, EventArgs e)
        {
            if (PWSecurityToken.Current != null)
            {
                try
                {
                    this.AppService.Logout(PWSecurityToken.Current.SecurityToken);
                }
                finally
                {
                    PWSecurityToken.Current = null;
                }
            }
            
            Response.Redirect(Globals.NavigateURL(this.TabId));
        }
    }
}
